package com.zhiyou100.dao.Impl;

import com.zhiyou100.JDBCUtil.DBUtil;
import com.zhiyou100.dao.RepairDao;
import com.zhiyou100.emtity.Repair;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-21 22:28
 **/
public class RepairDaoImpl implements RepairDao {

    private QueryRunner qur = new QueryRunner(DBUtil.getDataSource());

    @Override
    public List<Repair> queryAll() {
        String sql = "SELECT r.*,h.h_address,l.l_name,l.l_phone\n" +
                "FROM repair r\n" +
                "JOIN house h\n" +
                "ON r.h_id=h.h_id\n" +
                "JOIN logistics l\n" +
                "ON r.l_id=l.l_id\n" +
                "GROUP BY 1";
        try {
            List<Repair> query = qur.query(sql, new BeanListHandler<>(Repair.class));
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void add(Repair repair) {
        String sql = "insert into repair values(NULL,?,(SELECT l_id from logistics where l_name = ?) ,?,?,?,?)";
        try {
            System.out.println(repair);
            qur.update(sql, repair.getH_id(), repair.getL_name(), repair.getR_explain(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), repair.getR_result(), repair.getR_remark());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public Repair query(int id) {
        String sql = "SELECT r.*,h.h_address,l.l_name,l.l_phone\n" +
                "FROM repair r\n" +
                "JOIN house h\n" +
                "ON r.h_id=h.h_id\n" +
                "JOIN logistics l\n" +
                "ON r.l_id=l.l_id\n" +
                "where r.r_id = ?\n" +
                "GROUP BY 1";
        try {
            Repair query = qur.query(sql, new BeanHandler<>(Repair.class), id);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void delete(int id) {
        String sql = "delete from repair where r_id = ?";
        try {
            qur.update(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public void update(Repair query) {
        String sql = "update repair set h_id=?,l_id?,r_explain=?,r_remark=?,r_result=? where r_id=?";
        try {
            qur.update(sql, query.getH_id(),query.getL_id(),query.getR_explain(),query.getR_remark(),query.getR_result(),query.getR_id());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
